import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/product_electroncheckrecord.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class ProductElectroncheckrecordDialog extends StatefulWidget {
  ProductElectroncheckrecordDialog();

  @override
  _ProductElectroncheckrecordDialogState createState() =>
      _ProductElectroncheckrecordDialogState();
}

class _ProductElectroncheckrecordDialogState
    extends State<ProductElectroncheckrecordDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  ProductElectroncheckrecord productElectroncheckrecord =
      ProductElectroncheckrecord();

  //车间名称
  List<SelectOptionVO> workshopIdList = [];
  //设备工位
  List<SelectOptionVO> stationIdList = [];

  List<SelectOptionVO> stateList = [];

  DateFormat format = DateFormat('yyyy-MM-dd');
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    productElectroncheckrecord.checkdateStart = Utils.getDate();
    productElectroncheckrecord.checkdateEnd = Utils.getDate();
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      workshopIdList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "!=",
          val: null);
    } else {
      stationIdList = await DynamicDataApi.getModelSelect(
          model: "base.productionstation",
          name: "stationname",
          key: 'workshop_id',
          val: int.parse(userInfo.deptId!));
      productElectroncheckrecord.workshopId = userInfo.deptId;
    }
    stateList = [
      SelectOptionVO(label: '', value: ''),
      SelectOptionVO(label: '提交', value: '0'),
      SelectOptionVO(label: '处理', value: '1'),
    ];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            label: '检查开始时间',
            value: productElectroncheckrecord.createDateStart,
            lastDate: isNotBlank(productElectroncheckrecord.createDateEnd)
                ? format.parse(productElectroncheckrecord.createDateEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              productElectroncheckrecord.createDateStart = v;
              setState(() {});
            },
            onSaved: (v) {
              productElectroncheckrecord.createDateStart = v;
            },
          ),
          CrySelectDate(
            context,
            label: '检查结束时间',
            value: productElectroncheckrecord.createDateEnd,
            firstDate: isNotBlank(productElectroncheckrecord.createDateStart)
                ? format.parse(productElectroncheckrecord.createDateStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              productElectroncheckrecord.createDateEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              productElectroncheckrecord.createDateEnd = v;
            },
          ),
          userInfo.major != null
              ? CrySelect(
                  label: '车间名称',
                  dataList: workshopIdList,
                  value: productElectroncheckrecord.workshopId,
                  onSaved: (v) => {productElectroncheckrecord.workshopId = v},
                  onChange: (v) async {
                    if (productElectroncheckrecord.workshopId != v) {
                      formKey.currentState!.save();
                      productElectroncheckrecord.workshopId = v;
                      productElectroncheckrecord.stationId = null;
                      try {
                        stationIdList = await DynamicDataApi.getModelSelect(
                            active: false,
                            model: "base.productionstation",
                            name: "stationname",
                            key: 'workshop_id',
                            val: int.parse(v));
                      } catch (e) {
                        stationIdList = [];
                      }
                      setState(() {});
                    }
                  },
                )
              : CryInput(
                  label: '生产车间',
                  enable: false,
                  value: userInfo.deptName,
                ),
          CrySelect(
            label: '设备工位',
            dataList: stationIdList,
            value: productElectroncheckrecord.stationId,
            onSaved: (v) => {productElectroncheckrecord.stationId = v},
          ),
          CrySelect(
            label: '状态',
            dataList: stateList,
            value: productElectroncheckrecord.status,
            onSaved: (v) {
              productElectroncheckrecord.status = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, productElectroncheckrecord);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
